﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_SicoNidePalla : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        handleEditMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm() != "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('" + ValidateForm() + "');", true);
            return;
        }
        if (btnSave.CommandArgument == "")
        {
            tbl_siconide_palla model = new tbl_siconide_palla();
            model.Mill = Convert.ToInt32(ddlMill.SelectedValue.Trim());
            model.Feed_Rate = Convert.ToDecimal(txtFeed_Rate.Text.Trim());
            model.RPM = Convert.ToDecimal(txtRPM.Text.Trim());
            model.Room_Temperature = Convert.ToDecimal(txtRoom_Temperature.Text.Trim());
            model.Palla_humidity = Convert.ToDecimal(txtPalla_humidity.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.Previous_Process = ddlPrevious_Process.SelectedValue.Trim();
            model.CHARGE_Number =Convert.ToDecimal(ddlCHARGE_Number.SelectedValue.Trim());
            model.FLOWBIN_Number  = Convert.ToDecimal(ddlFLOWBIN_Number.SelectedValue.Trim());
            model.MILLJET_RUN_NUMBER = ddlMILLJET_RUN_NUMBER.SelectedValue.Trim();
            model.PALLA_RUN_NUMBER = txtPALLA_RUN_NUMBER.Text.Trim();
            model.CREATED_BY = User.Identity.Name;
            model.CREATED_DATE = DateTime.Now;



            BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
            try
            {


                
                bll.insert(model);
                string script = "top.location=" + '"' + HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + Request.RawUrl + '"';
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Success", "alert('Record Saved Succesfully');", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Postback", script, true);

                return;
            }
            catch (Exception)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Error", "alert('Error Occured');", true);
                return;
            }
        }
        else
        {
            tbl_siconide_palla model = new tbl_siconide_palla();
            model.ID = Convert.ToInt32(btnSave.CommandArgument);
            model.Mill = Convert.ToInt32(ddlMill.SelectedValue.Trim());
            model.Feed_Rate = Convert.ToDecimal(txtFeed_Rate.Text.Trim());
            model.RPM = Convert.ToDecimal(txtRPM.Text.Trim());
            model.Room_Temperature = Convert.ToDecimal(txtRoom_Temperature.Text.Trim());
            model.Palla_humidity = Convert.ToDecimal(txtPalla_humidity.Text.Trim());
            model.Remark = txtRemark.Text.Trim();
            model.Previous_Process = ddlPrevious_Process.SelectedValue.Trim();
            model.CHARGE_Number = Convert.ToDecimal(ddlCHARGE_Number.SelectedValue.Trim());
            model.FLOWBIN_Number = Convert.ToDecimal(ddlFLOWBIN_Number.SelectedValue.Trim());
            model.MILLJET_RUN_NUMBER = ddlMILLJET_RUN_NUMBER.SelectedValue.Trim();
            model.PALLA_RUN_NUMBER = txtPALLA_RUN_NUMBER.Text.Trim();
            model.UPDATED_BY = User.Identity.Name;
            model.UPDATED_DATE = DateTime.Now;



            BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
            bll.update(model);
            btnSave.CommandArgument = "";
            Response.Redirect(Request.Url.AbsolutePath);
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.AbsolutePath);
    }

    protected void linkDelete_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        tbl_siconide_palla model = new tbl_siconide_palla();
        model.ID = Convert.ToInt32(id); ;

        BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
        bll.delete(model);
    }

    protected void linkEdit_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        Response.Redirect(Request.Url.AbsolutePath + "?editRecord=" + id);
    }


    protected void handleEditMode()
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["editRecord"] != null)
            {
                string id = Request.QueryString["editRecord"];
                BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
                List<tbl_siconide_palla> list = bll.select(Convert.ToInt32(id));
                foreach (tbl_siconide_palla tbl in list)
                {
                    btnSave.CommandArgument = tbl.ID.ToString();
                    ddlMill.SelectedValue = tbl.Mill.ToString();
                    txtFeed_Rate.Text = tbl.Feed_Rate.ToString();
                    txtRPM.Text = tbl.RPM.ToString();
                    txtRoom_Temperature.Text = tbl.Room_Temperature.ToString();
                    txtPalla_humidity.Text = tbl.Palla_humidity.ToString();
                    txtRemark.Text = tbl.Remark;
                    ddlPrevious_Process.SelectedValue = tbl.Previous_Process;
                    if (ddlPrevious_Process.SelectedIndex > 0)
                    {
                        
                        DataTable dt = bll.GetChargeNumber(ddlPrevious_Process.SelectedValue.Trim());
                        ddlCHARGE_Number.DataSource = dt;
                        ddlCHARGE_Number.DataTextField = dt.Columns["Charge_Number"].ToString();
                        ddlCHARGE_Number.DataValueField = dt.Columns["Charge_Number"].ToString();
                        ddlCHARGE_Number.DataBind();
                        ListItem li = new ListItem("-- Select --", "0");
                        ddlCHARGE_Number.Items.Insert(0, li);
                        ddlCHARGE_Number.SelectedValue = tbl.CHARGE_Number.ToString();

                    }
                    if (ddlCHARGE_Number.SelectedIndex > 0)
                    {
                        
                        DataTable dt = bll.GetFlowbinNumber(ddlPrevious_Process.SelectedValue.Trim());
                        ddlFLOWBIN_Number.DataSource = dt;
                        ddlFLOWBIN_Number.DataTextField = dt.Columns["Flowbin_Number"].ToString();
                        ddlFLOWBIN_Number.DataValueField = dt.Columns["Flowbin_Number"].ToString();
                        ddlFLOWBIN_Number.DataBind();
                        ListItem li = new ListItem("-- Select --", "0");
                        ddlFLOWBIN_Number.Items.Insert(0, li);
                        ddlFLOWBIN_Number.SelectedValue = tbl.FLOWBIN_Number.ToString();
                    }
                    if (ddlFLOWBIN_Number.SelectedIndex > 0)
                    {
                        
                        DataTable dt = bll.GetMillJet_Run_Number();
                        if (ddlPrevious_Process.SelectedValue.Trim() == "Milljet")
                        {
                            ddlMILLJET_RUN_NUMBER.DataSource = dt;
                            ddlMILLJET_RUN_NUMBER.DataTextField = dt.Columns["MILLJET_RUN_NUMBER"].ToString();
                            ddlMILLJET_RUN_NUMBER.DataValueField = dt.Columns["MILLJET_RUN_NUMBER"].ToString();
                            ddlMILLJET_RUN_NUMBER.DataBind();
                        }
                        ListItem li = new ListItem("-- Select --", "0");
                        ddlMILLJET_RUN_NUMBER.Items.Insert(0, li);
                        ddlMILLJET_RUN_NUMBER.SelectedValue = tbl.MILLJET_RUN_NUMBER;


                    }
                    
                    
                    txtPALLA_RUN_NUMBER.Text = tbl.PALLA_RUN_NUMBER;
                    
                   

                }
            }
        }
    }



    protected void ddlMill_DataBound(object sender, EventArgs e)
    {
        ListItem li = new ListItem("--Select--", "0");
        ddlMill.Items.Insert(0, li);
    }
    protected void ddlPrevious_Process_SelectedIndexChanged(object sender, EventArgs e)
    {

        ddlCHARGE_Number.Items.Clear();
        ddlFLOWBIN_Number.Items.Clear();
        ddlMILLJET_RUN_NUMBER.Items.Clear();
        if (ddlPrevious_Process.SelectedIndex > 0)
        {
            BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
            DataTable dt = bll.GetChargeNumber(ddlPrevious_Process.SelectedValue.Trim());
            ddlCHARGE_Number.DataSource = dt;
            ddlCHARGE_Number.DataTextField = dt.Columns["Charge_Number"].ToString();
            ddlCHARGE_Number.DataValueField = dt.Columns["Charge_Number"].ToString();
            ddlCHARGE_Number.DataBind();
            ListItem li = new ListItem("-- Select --", "0");
            ddlCHARGE_Number.Items.Insert(0, li);
            return;

        }
    }
    protected void ddlCHARGE_Number_SelectedIndexChanged(object sender, EventArgs e)
    {
        
        ddlFLOWBIN_Number.Items.Clear();
        ddlMILLJET_RUN_NUMBER.Items.Clear();
        if (ddlCHARGE_Number.SelectedIndex > 0)
        {
            BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
            DataTable dt = bll.GetFlowbinNumber(ddlPrevious_Process.SelectedValue.Trim());
            ddlFLOWBIN_Number.DataSource = dt;
            ddlFLOWBIN_Number.DataTextField = dt.Columns["Flowbin_Number"].ToString();
            ddlFLOWBIN_Number.DataValueField = dt.Columns["Flowbin_Number"].ToString();
            ddlFLOWBIN_Number.DataBind();
            ListItem li = new ListItem("-- Select --", "0");
            ddlFLOWBIN_Number.Items.Insert(0, li);
            return;
        }
    }
    protected void ddlFLOWBIN_Number_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlMILLJET_RUN_NUMBER.Items.Clear();
        if (ddlFLOWBIN_Number.SelectedIndex > 0)
        {
            BLL_tbl_siconide_palla bll = new BLL_tbl_siconide_palla();
            DataTable dt = bll.GetMillJet_Run_Number();
            if (ddlPrevious_Process.SelectedValue.Trim() == "Milljet")
            {
                ddlMILLJET_RUN_NUMBER.DataSource = dt;
                ddlMILLJET_RUN_NUMBER.DataTextField = dt.Columns["MILLJET_RUN_NUMBER"].ToString();
                ddlMILLJET_RUN_NUMBER.DataValueField = dt.Columns["MILLJET_RUN_NUMBER"].ToString();
                ddlMILLJET_RUN_NUMBER.DataBind();
            }
                ListItem li = new ListItem("-- Select --", "0");
                ddlMILLJET_RUN_NUMBER.Items.Insert(0, li);
                return;
            

        }
    }

    #region Validate Form

    private string ValidateForm()
    {
        string str = "";
        try
        {
            // Check if Client is Selected 


            if (ddlMill.SelectedIndex == 0) return "Please select Mill";

            if (txtFeed_Rate.Text == "") return "Please enter Feed Rate";
            if (txtRPM.Text == "") return "Please enter RPM";
            if (txtRoom_Temperature.Text == "") return "Please enter Room Temperature";
            if (txtPalla_humidity.Text == "") return "Please enter Palla humidity";
            if (txtRemark.Text == "") return "Please enter Remark";
            if (ddlPrevious_Process.SelectedIndex == 0) return "Please select Previous Process";
            if (ddlCHARGE_Number.SelectedIndex == 0) return "Please select CHARGE Number";
            if (ddlFLOWBIN_Number.SelectedIndex == 0) return "Please select FLOWBIN Number";
            if (txtPALLA_RUN_NUMBER.Text == "") return "Please enter PALLA RUN #";

            return str;
        }
        catch (Exception)
        {

            return "Error in Validation";
        }
        finally
        {
            str = null;
        }
    }

    #endregion
}